package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mybatisflex.core.query.QueryWrapper;
import com.qingzhou.system.domain.SysDictData;
import com.qingzhou.system.service.ISysDictDataService;
import com.qingzhou.web.repository.entity.HistoryDiseaseDetail;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.ArrayList;
import java.util.List;

public class HistoryDiseaseDetailConverter implements Converter<List<HistoryDiseaseDetail>> {
    @Override
    public WriteCellData<?> convertToExcelData(List<HistoryDiseaseDetail> value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if(CollUtil.isEmpty(value)){
            WriteCellData<Object> emptyData = new WriteCellData<>("");
            emptyData.setType(CellDataTypeEnum.STRING);
            return emptyData;
        }

        return new WriteCellData<>(this.getConvertString(value));
    }

    public String getConvertString(List<HistoryDiseaseDetail> value){
        if(CollUtil.isEmpty(value)) return "";

        List<String> rows = new ArrayList<>();
        ISysDictDataService dictDataService = Solon.context().getBean(ISysDictDataService.class);
        for(HistoryDiseaseDetail item : value){
            String type = item.getType();
            SysDictData condition = new SysDictData();
            condition.setDictValue(type);
            condition.setDictType("past_diseases_dict");
            SysDictData dictData = dictDataService.getMapper().selectOneByQuery(QueryWrapper.create(condition));
            String text = item.getHistoryDisOtherCancerText()+item.getHistoryDisOtherText();
            if(StrUtil.isNotEmpty(text)){
                text = "明细："+text+"，";
            }
            if(ObjUtil.isNotNull(dictData)){
                rows.add(StrUtil.format("类型：{}，{}发病年余：{}，治疗：{}",dictData.getDictLabel(),text,item.getOnsetYear(),item.getPastTreatment()));
            }else{
                rows.add(StrUtil.format("类型：{}，{}发病年余：{}，治疗：{}",type,text,item.getOnsetYear(),item.getPastTreatment()));
            }
        }
        return CollUtil.join(rows,"\n");
    }


}
